<template>
  <div>
    <BasicTable @register="registerTable">
      <template #headerTop>
        <Tabs v-model:activeKey="activeKey" type="card" @change="handleTabChange">
          <TabPane v-for="v in tabsData" :key="v.key" :tab="v.title"></TabPane>
        </Tabs>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '订单详情',
                tooltip: '订单详情',
                onClick: handleDetail.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
      <!-- <template #toolbar>
        <a-button type="primary">导出订单</a-button>
      </template> -->
    </BasicTable>
  </div>
</template>
<script lang="ts" setup>
  import { Tabs, TabPane } from 'ant-design-vue';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { getBasicColumns } from './tableData';
  import Dictionary from '@/utils/dictionary';
  import { getActivityTicketOrderList } from '@/api/membership';
  import { IOrderListResult } from '@/api/membership/type';
  import { useRouter } from 'vue-router';
  import { getActivityOrderStatus } from '@/utils/orderStatusText';
  import { reactive, ref } from 'vue';
  import { TicketTypeEnum } from '@/enums/orderEnums';

  const dicts = Dictionary.getLocalMulti(['ticketType', 'refundStatus', 'orderState', 'payMode']);
  const router = useRouter();

  const activeKey = ref();
  const tabsData = reactive([
    {
      title: '全部订单',
      key: '',
      handleReload() {
        reload({ filterInfo: { ticketType: activeKey.value } });
      },
    },
    {
      title: '付费订单',
      key: TicketTypeEnum.pay,
      handleReload() {
        reload({ filterInfo: { ticketType: activeKey.value } });
      },
    },
    {
      title: '0元订单',
      key: TicketTypeEnum.free,
      handleReload() {
        reload({ filterInfo: { ticketType: activeKey.value } });
      },
    },
  ]);

  const handleTabChange = (val: string) => {
    tabsData[val].handleReload();
  };

  const [registerTable, { reload }] = useTable({
    title: '开启搜索区域',
    api: (obj) => {
      obj.ticketType = activeKey.value;
      return getActivityTicketOrderList(obj);
    },
    columns: getBasicColumns(),
    formConfig: {
      labelWidth: 100,
      schemas: [
        {
          field: `keyWord`,
          label: `关键字`,
          component: 'Input',
          componentProps: {
            placeholder: '输入订单编号/活动名称/类型名称/项目归属/订单来源',
          },
          colProps: {
            span: 7,
          },
        },
        {
          field: `orderState`,
          label: `订单状态`,
          component: 'Select',
          componentProps: {
            fieldNames: { label: 'dictLabel', value: 'dictValue' },
            options: dicts.orderState,
          },
          colProps: {
            span: 4,
          },
        },
        {
          field: `payTime`,
          label: `付款时间`,
          component: 'RangePicker',
          colProps: {
            span: 6,
          },
        },
      ],
    },
    beforeFetch: (parmas) => {
      let payTime = parmas.payTime || [];
      return {
        ...parmas,
        startTime: payTime[0],
        endTime: payTime[1],
        payTime: undefined,
      };
    },
    useSearchForm: true,
    showTableSetting: true,
    tableSetting: { fullScreen: true },
    showIndexColumn: true,
    clickToRowSelect: false,
    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action',
    },
    afterFetch: (data: IOrderListResult[]) => {
      return data.map((item) => {
        return {
          ...item,
          orderStateText: getActivityOrderStatus(item.orderState, item.payState, item.refundStatus)
            .name,
          ticketType: dicts.ticketType.find((v) => v.dictValue == item.orderState)?.dictLabel,
          payMode: dicts.payMode.find((v) => v.dictValue == item.payMode)?.dictLabel,
        };
      });
    },
  });

  function handleDetail(row: IOrderListResult) {
    router.push({
      path: '/membership/activityOrderDetail',
      query: { id: row.id },
    });
  }
</script>
